using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace net_work.Common.Result;

public class AjaxResult
{
    [JsonPropertyName("code")] public int Code { get; set; }
    [JsonPropertyName("message")] public string Message { get; set; }
    [JsonPropertyName("data")] public object? Data { get; set; }
    [JsonPropertyName("timestamp")] public long Timestamp { get; set; }

    private AjaxResult(AjaxCode code, string message, object? data = null)
    {
        Code = (int)code;
        Message = message;
        Data = data;
        Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
    }

    private AjaxResult(int code, string message, object? data)
    {
        Code = code;
        Message = message;
        Data = data;
        Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
    }

    /// <summary>
    /// 操作成功
    /// </summary>
    /// <param name="message">返回消息</param>
    /// <param name="data">返回数据</param>
    /// <returns>AjaxResult</returns>
    public static OkObjectResult Success(string message = "操作成功", object? data = null)
    {
        return new OkObjectResult(new AjaxResult(AjaxCode.Success, message, data));
    }

    /// <summary>
    /// 操作成功
    /// </summary>
    /// <param name="data">返回数据</param>
    /// <returns>AjaxResult</returns>
    public static OkObjectResult Success(object data)
    {
        return new OkObjectResult(new AjaxResult(AjaxCode.Success, "操作成功", data));
    }

    /// <summary>
    /// 操作失败
    /// </summary>
    /// <param name="message">返回消息</param>
    /// <param name="data">返回数据</param>
    /// <returns>AjaxResult</returns>
    public static OkObjectResult Error(string message = "操作失败", object? data = null)
    {
        return new OkObjectResult(new AjaxResult(AjaxCode.Error, message, data));
    }

    /// <summary>
    /// 自定义返回码
    /// </summary>
    /// <param name="code">返回码</param>
    /// <param name="message">返回消息</param>
    /// <param name="data">返回数据</param>
    /// <returns>AjaxResult</returns>
    public static OkObjectResult Custom(int code, string message, object? data = null)
    {
        return new OkObjectResult(new AjaxResult(code, message, data));
    }

    /// <summary>
    /// 自定义返回码
    /// </summary>
    /// <param name="code">返回码</param>
    /// <param name="message">返回消息</param>
    /// <param name="data">返回数据</param>
    /// <returns>AjaxResult</returns>
    public static OkObjectResult Custom(AjaxCode code, string message, object? data = null)
    {
        return new OkObjectResult(new AjaxResult(code, message, data));
    }
}